package com.kws.commercial.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kws.commercial.dao.CommercialSignatureManage;
import com.kws.commercial.dao.Info.QueryCommercialSignatureInfo;
import com.kws.commercial.dao.Rq.CreateCommercialSignatureRq;
import com.kws.commercial.dao.Rq.UpdateCommercialSignatureRq;
import com.kws.commercial.mapper.CommercialSignatureManageMapper;
import com.kws.commercial.service.CommercialSignatureManageService;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>CommercialSignatureManageServiceImpl</b>
 *
 * @description: CommercialSignatureManageServiceImpl <br>
 * @date: 2024/10/24 15:15 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class CommercialSignatureManageServiceImpl extends ServiceImpl<CommercialSignatureManageMapper, CommercialSignatureManage> implements CommercialSignatureManageService {
    private static final Logger logger = LoggerFactory.getLogger(CommercialSignatureManageServiceImpl.class);

    @Override
    public PageInfo<QueryCommercialSignatureInfo> queryCommercialSignaturePage(Integer pageNum, Integer pageSize, String signatureHolder) {
        PageHelper.startPage(pageNum,pageSize);
        Long companyId = SecurityUtils.getCompanyId();
        List<QueryCommercialSignatureInfo> signatureList = baseMapper.queryCommercialSignaturePage(signatureHolder,companyId);
        PageInfo<QueryCommercialSignatureInfo> pageInfo = new PageInfo<>(signatureList);
        return pageInfo;
    }

    @Override
    public boolean createCommercialSignature(CreateCommercialSignatureRq rq) {
        CommercialSignatureManage commercialSignatureManage = new CommercialSignatureManage();
        commercialSignatureManage.setSignatureHolder(rq.getSignatureHolder());
        commercialSignatureManage.setCompanyId(SecurityUtils.getCompanyId());
        commercialSignatureManage.setSignatureUrl(rq.getSignatureUrl());
        Ksys.setBasicModelData(commercialSignatureManage, SecurityUtils.getUserId().toString(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(commercialSignatureManage);
        if (1 == insert){
            return true;
        }
        return false;
    }

    @Override
    public QueryCommercialSignatureInfo queryCommercialSignatureInfo(Long id) {
        return baseMapper.queryCommercialSignatureInfo(id);
    }

    @Override
    public boolean updateCommercialSignatureById(UpdateCommercialSignatureRq rq) {
        CommercialSignatureManage commercialSignatureManage = baseMapper.selectById(rq.getId());
        if (null != commercialSignatureManage){
            commercialSignatureManage.setSignatureUrl(rq.getSignatureUrl());
            commercialSignatureManage.setSignatureHolder(rq.getSignatureHolder());
            Ksys.setBasicModelData(commercialSignatureManage,SecurityUtils.getUserId().toString(),KConstants.DATA_ENABLE,ModelOpType.UPDATE);
            int i = baseMapper.updateById(commercialSignatureManage);
            if (i == 1){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean deleteCommercialSignatureById(List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.in("id",ids);
            updateWrapper.set("enable","0");
            updateWrapper.set("update_user",SecurityUtils.getUserId().toString());
            updateWrapper.set("update_time",new Date());
            int update = baseMapper.update(null, updateWrapper);
            if (update > 0){
                return true;
            }
        }
        return false;
    }
}
